import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    //所有信息
    allData: [],
    //楼栋信息
    buildData: [],
    //楼栋数量
    buildCount: "",
    roomCount: "",
    householdCount: "",
    moveInCount: "",
    moveCount: "",
    rentCount: "",
    selfCount: "",
    idleCount: "",
    //所有有住户的房间
    OwnerRoom: []
  },
  mutations: {
    changeBuildData(store, data) {
      store.buildData = data
      store.buildCount = data.length
      // data.forEach(item => {
      //   store.allData.forEach(items=>{
      //     if(items.Status=="已入住" && items.BuildingName==item.BuildingName){

      //       console.log(123);

      //     }
      //   })
      // })
    },
    changeAllData(store, data) {
      store.moveInCount = 0
      store.selfCount = 0
      store.rentCount = 0
      store.roomCount = 0
      store.moveCount = 0
      store.idleCount = 0
      store.householdCount = 0
      store.OwnerRoom = 0
      store.allData = data
      data.forEach(item => {
        if (item.Status == "已入住") {
          store.moveInCount++
        }
        if (item.Usage == "自用") {
          store.selfCount++
        }
        if (item.Usage == "租用") {
          store.rentCount++
        }
        // store.state.buildData.forEach(item=>{
        //   console.log(item.BuildingName);

        // })
      });
      store.roomCount = store.allData.length
      store.moveCount = store.roomCount - store.moveInCount
      store.idleCount = store.roomCount - store.rentCount - store.selfCount
      store.householdCount = store.roomCount - store.idleCount
      //获取到数据后对数据进行过滤
      store.OwnerRoom = data.filter(item => item.Owner != "")
    },
  },
  actions: {

    //楼栋数量 房屋数量 住户数量 已入住 未入住 租用 自用 闲置
    //所有的信息
    getList() {
      fetch('http://127.0.0.1/user/all', {
        method: 'GET', // 请求方法
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('token')}`,
          'Content-Type': 'application/json' // 如果需要发送 JSON 数据，则包含此行 // 设置请求头
        }
      }).then(res => res.json()).then(res1 => {
        store.commit('changeAllData', res1)
      
      }).catch(err => {
        console.log(err);
      })
      
    },
    getBuild() {
      fetch('http://127.0.0.1/build/all', {
        method: 'GET', // 请求方法
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('token')}`,
          'Content-Type': 'application/json' // 如果需要发送 JSON 数据，则包含此行 // 设置请求头
        }
      }).then(res => res.json()).then(res1 => {
        this.commit('changeBuildData', res1)
      }).catch(err => {
        console.log(err);
      })
    }
  }
})

export default store